﻿/**
 * 请不要将该类库用于非法途径和商业途径，与此产生的一切法律后果与本人无关。
 * 有任何问题，可以访问我的博客：http://baiyafeng.com 留言
 * 使用本类库请遵循GPL v3协议。
 **/

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace SinaBlogQiangShafa
{
  public    class DuanUrl
    {
      private   bool _successful = false;
 

      public   bool Success
      {
          get { return _successful; }
      }
       
      public   string BianDuan(string address)
      {
          WebRequest req = null;
          string url = "";
          try
          {
              req = WebRequest.Create(@"http://duanurl.com/duanurl/api-create.php?"+
                  "url="+address);
              req.Method = "GET";
              WebResponse res = req.GetResponse();
              Stream s = res.GetResponseStream();
              StreamReader streamRead = new StreamReader(s);
              Char[] readBuff = new Char[256];
              int count = streamRead.Read(readBuff, 0, 256);

              while (count > 0)
              {
                  String outputData = new String(readBuff, 0, count);
                  url += (outputData);
                  count = streamRead.Read(readBuff, 0, 256);
              }
              s.Close();
              streamRead.Close();
              _successful = true;
          }
          catch (Exception error)
          {
              Log.WriteError(string.Format("[{0}]:{1}", DateTime.Now.ToString(), error.ToString()));
              _successful = false;
          }
          return url;
      }
    }
}
